package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * @author sunli
 * @date 2024/7/9 10:46
 * @desc
 **/
public class RandomUtils {
    /**
     *
     *  年月日时分秒
     */
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");

    /**
     *
     *  年月日
     */
    private static SimpleDateFormat ymsDateFormat = new SimpleDateFormat("yyyyMMdd");
    /**
     *
     * 生成6位随机数
     */
    public static int genBatchNo() {
        Random r = new Random( System.currentTimeMillis() );
        return 100000 + r.nextInt(999999);

    }

    /**
     *
     * 生成8位随机数
     */
    public static int genEightCode() {
        Random r = new Random( System.currentTimeMillis() );
        return 10000000 + r.nextInt(99999999);

    }
    /**
     *
     * 年月日时分秒+随机字符串
     */
    public static String genCode(Date date) {
//        String format = simpleDateFormat.format(date);
        String format = simpleDateFormat.format(date);
        Random r = new Random( System.currentTimeMillis() );
        Integer l = 1000000 + r.nextInt(99999999);
        return format+l;
    }

    /**
     *
     *  获取5位有序的code
     *
     */
    public static String getMemberNumber(String memberNumber){
        return test(memberNumber) ;
    }
    private static String test(String id){
        int LENGTH = 5 ;
        int max = 99999 ;
        String substring = id.substring(id.length()-5);
        String alpha = id.replace(substring,"");
        String timeNow = DateUtils.dateTimeNow();
        int parseInt = Integer.parseInt(substring);
        if(parseInt == max){
            return timeNow+ "00001" ;
        }else{
            parseInt +=1 ;

            String format = String.format("%s%0" + LENGTH + "d", alpha, parseInt);
            return timeNow +format ;
        }
    }

    /**
     * 生成uuid
     *
     * @param
     * @return 32位的UUID字符串
     */
    public static String uuid() {
        return UUID.randomUUID().toString().toUpperCase().replace("-", "");
    }


    /**
     *
     * @param orderSign  服务商订单唯一标识
     * @param orderSerialNumber 订单序列号
     * @param typeCode 机构 O 、代理 A 、商户 M
     * @return
     */
    public static String getGenEightCode(String orderSign,String typeCode, String orderSerialNumber) {
        String genEightCode;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        genEightCode =  sdf.format(new Date())+orderSerialNumber;
        if(StringUtils.isNotBlank(orderSign) && StringUtils.isNotBlank(typeCode)){
            return  orderSign+"_"+typeCode+genEightCode;
        }else{
            return genEightCode;
        }
    }

    public static void main(String[] args) {
        String genCode = RandomUtils.genCode(new Date());
        System.out.println(genCode);

//        String genEightCode = getGenEightCode(null, null, "11111");
//        log.debug(genEightCode);
    }

}
